package main;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author WIN7
 */
public class StoryModeMenuScreen extends GameCanvas implements CommandListener, Runnable{
    private Map map;
    private MainMidlet midlet;
    private Command cmdBack;
    private boolean running = true;
    public StoryModeMenuScreen(MainMidlet _midlet)
    {
        super(true);
        setFullScreenMode(true);
        midlet = _midlet;
        map = new Map();
        cmdBack = new Command("Back", Command.BACK, 0);
        addCommand(cmdBack);
        setCommandListener(this);
    }
    
    public void draw(Graphics g)
    {
        g.setColor(0, 0, 200);
        g.fillRect(0, 0, getWidth(), getHeight());
        map.paint(g);
        int y = getHeight() - 10;
        g.setColor(255, 0, 0);
        g.drawString("Back", 0, y, Graphics.BASELINE|Graphics.LEFT);
        flushGraphics();
    }

    public void commandAction(Command c, Displayable d) {
        if(c == cmdBack)
        {
            stop();
            midlet.showMainMenu();
        }
    }

    public void stop()
    {
        running = false;
    }
    
    public void start()
    {
        Thread t = new Thread(this);
        running = true;
        t.start();
        
    }
    
    private void updateState()
    {
        int keyState = getKeyStates();
        int key = -1;
        if((keyState & LEFT_PRESSED) != 0)
        {
            map.goToWesternItem();
        }else if((keyState & RIGHT_PRESSED) != 0)
        {
            map.goToEasternItem();
        }else if((keyState & UP_PRESSED) != 0)
        {
            map.goToNorthernItem();
        }else if((keyState & DOWN_PRESSED) != 0)
        {
            map.goToSouthernItem();
        }else if((keyState & FIRE_PRESSED) != 0)
        {
            Item item = map.getCurrentItem();
            Character character = map.getCharacter();
            stop();
            midlet.showStoryModeMenuOption(item, character);
        }
        map.updateState();
        map.animation();
    }
    
    public void run() {
        Graphics g = getGraphics();
        while(running)
        {
            updateState();
            draw(g);
            try
            {
                Thread.sleep(100);
            }
            catch(Exception ex)
            {
                ex.printStackTrace();
                return;
            }
        }
    }
}
